// https://leetcode.cn/problems/guess-number-higher-or-lower-ii/description/

// 算法思路总结：
// 1. 记忆化搜索求解猜数字最小代价问题
// 2. 选择每个数字作为猜测点计算最坏情况代价
// 3. 取所有猜测策略中的最小最大代价
// 4. 递归分割区间并缓存结果避免重复计算
// 5. 时间复杂度：O(n³)，空间复杂度：O(n²)

#include <iostream>
using namespace std;

#include <vector>
#include <climits>
#include <cstring>
#include <algorithm>

class Solution 
{
public:
    int memo[201][201];

    int getMoneyAmount(int n) 
    {
        memset(memo, 0, sizeof(memo));

        return dfs(1, n);
    }

    int dfs(int left, int right)
    {
        if (left >= right)
        {
            return 0;
        }

        if (memo[left][right] != 0)
        {
            return memo[left][right];
        }

        int ret = INT_MAX;
        for (int head = left ; head <= right ; head++)
        {
            int x = dfs(left, head - 1);
            int y = dfs(head + 1, right);
            ret = min(ret, head + max(x, y));
        }

        return memo[left][right] = ret;
    }
};

int main()
{
    int n1 = 10, n2 = 2;
    Solution sol;

    cout << sol.getMoneyAmount(n1) << endl;
    cout << sol.getMoneyAmount(n2) << endl;

    return 0;
}